//
//  AppDelegate.m
//  Haliaeetus
//
//  Created by junli hou on 2018/3/20.
//  Copyright © 2018年 BMY. All rights reserved.
//

#import "AppDelegate.h"
#import "MainTabbarViewController.h"
#import <UMShare/UMShare.h>
#import <UMCommon/UMCommon.h>
#import "WXApi.h"
#import <CoreLocation/CoreLocation.h>
@interface AppDelegate ()<WXApiDelegate>
@property (strong,nonatomic) CLLocationManager * locationManager;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
     [WXApi registerApp:WeChatAppId];
    [self setNavigationRootController];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    [self initCLLocationManager];
    return YES;
}

- (void)initCLLocationManager
{
    //定位服务是否可用
    BOOL enable=[CLLocationManager locationServicesEnabled];
    //是否具有定位权限
    int status=[CLLocationManager authorizationStatus];
    if(!enable || status<3)
    {
        //请求权限
        if ([[UIDevice currentDevice].systemVersion floatValue] >= 8)
        {
            //由于IOS8中定位的授权机制改变 需要进行手动授权
            self.locationManager = [[CLLocationManager alloc] init];
            //获取授权认证
            [self.locationManager requestAlwaysAuthorization];
            [self.locationManager requestWhenInUseAuthorization];
        }
        
        
    }
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {


          return [WXApi handleOpenURL:url delegate:self];

    
}
- (void)onResp:(BaseResp*)resp
{
    NSString *strMsg = [NSString stringWithFormat:@"errcode:%d", resp.errCode];
    NSString *strTitle;
    if([resp isKindOfClass:[PayResp class]]){
        
        strTitle = [NSString stringWithFormat:@"支付结果"];
        
        switch (resp.errCode) {
            case WXSuccess:
                strMsg = @"支付结果：成功！";
                NSLog(@"支付成功－PaySuccess，retcode = %d", resp.errCode);
                break;
                
            default:
                strMsg = [NSString stringWithFormat:@"支付结果：失败！retcode = %d, retstr = %@", resp.errCode,resp.errStr];
                NSLog(@"错误，retcode = %d, retstr = %@", resp.errCode,resp.errStr);
                break;
        }
        [[NSNotificationCenter defaultCenter] postNotificationName:@"UpdateWXPay" object:resp];
        
    }else  if([resp isKindOfClass:[SendMessageToWXResp class]]){
        

        [[NSNotificationCenter defaultCenter] postNotificationName:@"UpdateWXShare" object:resp];
        
    }
    
}
- (void)setNavigationRootController {
    MainTabbarViewController *mainTabbarController = [[MainTabbarViewController alloc] init];
//    self.window.rootViewController = mainTabbarController;
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window setRootViewController:mainTabbarController];
    [self.window makeKeyAndVisible];
}


@end
